import React, { useState } from 'react';
import NumberField from '../../../../../../components/common/NumberField';
import StatefulCalculationCard from '../../../../../../components/common/StatefulCalculationCard';
import ExportData from '../../../../../../components/common/ExportCalculateData';
import type { ResultItem } from '../../../../../../../shared/types';

// 地基计算表单类型定义
export interface FoundationFormValues { 
    foundationLength: number,
    foundationWidth: number,
    foundationHeight: number,
    concreteDensity: number,
    safetyFactor: number,
    columnHeight: number,
    foundationBuriedDepth: number,
    soilUnitWeight: number,
    foundationInterfaceCoefficient: number,
    trussLateralLoad: number,
    columnWidth: number,
    columnDepth: number,
    columnElasticModulus: number,
    soilCohesion: number,
    soilFrictionAngle: number,
}

// 地基计算结果类型定义
export interface FoundationResult {
    volume: number;
    weight: number;
    weightWithSafety: number;
    surfaceArea: number;
    foundationLoad: number;
    foundationPressure: number;
    bearingCapacity: number;
    ultimateBearingCapacity: number;
    safetyCheck: boolean;
    columnHeight: number;
    foundationBuriedDepth: number;
    soilWeight: number;
    totalLoad: number;
    foundationInterfaceCoefficient: number;
    foundationCrossSectionArea: number;
    trussLateralLoad: number;
    columnBottomMoment: number;
    momentEffect: number;
    columnLateralStiffness: number;
    columnShearForce: number;
}

// 地基计算初始值配置
const initialValues: FoundationFormValues = {
    foundationLength: 1.4,
    foundationWidth: 1.2,
    foundationHeight: 0.6,
    concreteDensity: 2500.0,
    safetyFactor: 1.2,
    columnHeight: 3.0,
    foundationBuriedDepth: 1.1,
    soilUnitWeight: 1800.0,
    foundationInterfaceCoefficient: 1.0,
    trussLateralLoad: 20.0,
    columnWidth: 0.5,
    columnDepth: 0.5,
    columnElasticModulus: 210000000,
    soilCohesion: 10,
    soilFrictionAngle: 30,
};

// 地基计算计算卡片组件配置
const foundationConfig = {
  key: 'Foundation',
  label: '地基计算',
  formKey: 'crossing-foundation-calculation',
  initialValues: initialValues,
  calculationType: 'crossing-foundation-calculation',
  resultFormatter: (data: FoundationResult) => [
    { key: 'volume', name: '基础体积', value: data.volume, unit: 'm³' },
    { key: 'weight', name: '基础重量', value: data.weight, unit: 'kg' },
    { key: 'weightWithSafety', name: '基础重量(含安全系数)', value: data.weightWithSafety, unit: 'kg' },
    { key: 'surfaceArea', name: '基础表面积', value: data.surfaceArea, unit: 'm²' },
    { key: 'foundationLoad', name: '基础荷载', value: data.foundationLoad, unit: 'kN' },
    { key: 'foundationPressure', name: '基础压强', value: data.foundationPressure, unit: 'kPa' },
    { key: 'bearingCapacity', name: '允许承载力', value: data.bearingCapacity, unit: 'kPa' },
    { key: 'ultimateBearingCapacity', name: '极限承载力', value: data.ultimateBearingCapacity, unit: 'kPa' },
    { key: 'safetyCheck', name: '安全校核', value: data.safetyCheck ? '通过' : '不通过', unit: '' },
    { key: 'columnHeight', name: '柱子高度', value: data.columnHeight, unit: 'm' },
    { key: 'foundationBuriedDepth', name: '基础埋深', value: data.foundationBuriedDepth, unit: 'm' },
    { key: 'soilWeight', name: '土重', value: data.soilWeight, unit: 'kN' },
    { key: 'totalLoad', name: '总荷载', value: data.totalLoad, unit: 'kN' },
    { key: 'foundationInterfaceCoefficient', name: '基础界面系数', value: data.foundationInterfaceCoefficient, unit: '' },
    { key: 'foundationCrossSectionArea', name: '基础截面面积', value: data.foundationCrossSectionArea, unit: 'm²' },
    { key: 'trussLateralLoad', name: '桁架侧向荷载', value: data.trussLateralLoad, unit: 'kN' },
    { key: 'columnBottomMoment', name: '柱底弯矩', value: data.columnBottomMoment, unit: 'kN·m' },
    { key: 'momentEffect', name: '弯矩影响', value: data.momentEffect, unit: 'kPa' },
    { key: 'columnLateralStiffness', name: '柱抗侧刚度', value: data.columnLateralStiffness, unit: 'kN/m' },
    { key: 'columnShearForce', name: '柱水平剪力', value: data.columnShearForce, unit: 'kN' },
  ],
  renderFormFields: () => (
    <>
      <NumberField name="foundationLength" label="基础长度" required min={0} step={0.1} tooltip="基础长度 (m)" />
      <NumberField name="foundationWidth" label="基础宽度" required min={0} step={0.1} tooltip="基础宽度 (m)" />
      <NumberField name="foundationHeight" label="基础高度" required min={0} step={0.1} tooltip="基础高度 (m)" />
      <NumberField name="concreteDensity" label="钢筋混凝土密度" required min={0} step={100} tooltip="钢筋混凝土密度 (kg/m³)" />
      <NumberField name="safetyFactor" label="安全系数" required min={0} step={0.1} tooltip="安全系数" />
      <NumberField name="columnHeight" label="柱子高度" required min={0} step={0.1} tooltip="柱子高度 (m)" />
      <NumberField name="foundationBuriedDepth" label="基础埋深" required min={0} step={0.1} tooltip="基础埋深 (m)" />
      <NumberField name="soilUnitWeight" label="土重度" required min={0} step={100} tooltip="土重度 (kg/m³)" />
      <NumberField name="foundationInterfaceCoefficient" label="基础界面系数" required min={0} step={0.1} tooltip="基础界面系数" />
      <NumberField name="trussLateralLoad" label="桁架侧向荷载" required min={0} step={1} tooltip="桁架侧向荷载 (kN)" />
      <NumberField name="columnWidth" label="柱子宽度" required min={0} step={0.1} tooltip="柱子宽度 (m)" />
      <NumberField name="columnDepth" label="柱子深度" required min={0} step={0.1} tooltip="柱子深度 (m)" />
      <NumberField name="columnElasticModulus" label="柱子弹性模量" required min={0} step={1000000} tooltip="柱子弹性模量 (kPa)" />
      <NumberField name="soilCohesion" label="土的粘聚力" required min={0} step={1} tooltip="土的粘聚力 (kPa)" />
      <NumberField name="soilFrictionAngle" label="土的内摩擦角" required min={0} step={1} tooltip="土的内摩擦角 (度)" />
    </>
  )
};

// 地基计算计算卡片组件实现
const StatisticsCard: React.FC = () => {
  const [lastResults, setLastResults] = useState<ResultItem[] | null>(null);
  
  return (
    <StatefulCalculationCard
      title={foundationConfig.label}
      formKey={foundationConfig.formKey}
      initialValues={foundationConfig.initialValues}
      renderFormFields={foundationConfig.renderFormFields}
      calculationType={foundationConfig.calculationType}
      resultFormatter={(data) => {
        const results = foundationConfig.resultFormatter(data);
        setLastResults(results);
        return results;
      }}
      columnCount={3} // 使用3列布局
      tableHeight={500} // 设置表格高度为500px
      exportComponent={(form) => (
        <ExportData
          siteName={foundationConfig.label}
          inputs={form.getFieldsValue()}
          results={lastResults ? Object.fromEntries(lastResults.map(r => [r.key, r.value])) : {}}
          calculationType={foundationConfig.calculationType}
        />
      )}
    />
  );
};

export default StatisticsCard;